<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title></title>
<script type="text/javascript">

	$(function(){
		$('.isPrimary').iCheck({
		  checkboxClass: 'icheckbox_flat-blue',
	      radioClass: 'iradio_flat-blue'
		});
	})
	
	// 商家是否启用分仓
	function updateDistributeState() {
		$.ajax({
		    type : 'post',
			url : '${ctx}/v2/customers/usingDistributeWarehouse',
			data : {
			  "customerCode":'${customer.customerCode}',
			  "type":$("input[name='isBranch']:checked").val()
			},
			// dataType : 'json',
			success : function(data) {
			  	if(data=='success') {
			  	  	alert("修改成功");
			  	} else {
			  		alert("修改失败,请稍后再试");
			  	}
			},
			error : function(data) {
			  	alert("网络繁忙，请稍后重试...","温馨提示");
			}
		});
	}
	
	// 商家库存查询类型
	function updateStockQueryType(){
		  $.ajax({
		    type : 'post',
			url : '${ctx}/v2/customers/updateStockQueryType',
			data : {
			  "customerCode":'${customer.customerCode}',
			  "type":$("input[name='stockQueryType']:checked").val()
			},
			// dataType : 'json',
			success : function(data) {
			  	if(data=='success') {
			  	  	alert("修改成功");
			  	} else {
			  		alert("修改失败,请稍后再试");
			  	}
			},
			error : function(data) {
			  	alert("网络繁忙，请稍后重试...","温馨提示");
			}
		});
	}
	
	// 绑定仓库
	function bindWarehouse(warehouseCode,btn){
	  	var $btn=$(btn);
		$.ajax({
		    type : 'post',
			url : '${ctx}/v2/customers/bindWarehouse',
			data : {
			  "customerCode":'${customer.customerCode }',
			  "warehouseCode":warehouseCode
			},
			success : function(data) {
			  if(data=='success') {
			    	$btn.removeAttr("onclick");
			    	$btn.removeAttr("class").addClass('btn disabled bind');
			    	$btn.removeAttr("style").attr("style","background-color: gray");
			    	$btn.text('已绑定');
			  	  	alert("修改成功");
			  	} else {
			  		alert("修改失败");
			  	}
			},
			error : function(data) {
			  	alert("网络繁忙，请稍后重试...","温馨提示");
			}
		});
	}
		
	// 绑定主仓
	function addPrimaryWarehouse(){
	  	$('.radio.isPrimary:checked').val();
	  	$('.radio.isPrimary:checked').closest('tr').find(".bind").text();
	  	
	  	if(typeof($('.radio.isPrimary:checked').val()) != 'undefined' &&  $('.radio.isPrimary:checked').closest('tr').find(".bind").text() == '已绑定'){
	  		$.ajax({
			    type : 'post',
				url : '${ctx}/v2/customers/bindPrimaryWarehouse',
				data : {
					'warehouseCode':$('.radio.isPrimary:checked').val(),
					'customerCode':'${customer.customerCode}'
				},
				success : function(data) {
				  if(data=='success') {
				  	  	alert("修改成功");
				  	} else {
				  		alert("修改失败");
				  	}
				},
				error : function(data) {
				  	alert("网络繁忙，请稍后重试...","温馨提示");
				}
			});
	  	} else {
	  		alert("主仓必须为已绑定的分仓");
			return;
	  	}
	}
	
	var parentWarehouseCode = '';
	
	// 在模态窗弹出前添加数据
	function modeTransfer(mainCode,btn){
		var $btn = $(btn);
		if($btn.parent('td').find('.bind').text() != '已绑定') {
			alert("请先绑定此仓库");
			return;
		}
		parentWarehouseCode = mainCode;
		// 之前的流转仓数据
		var transferList = $btn.closest('tr').find('.transferList').text();
		var arr1 = transferList.split(",");
		var arr2 =new Array();
		for(var i=0;i<arr1.length;i++){
		  	if(arr1[i] != ""){
		  		arr2[i]=arr1[i].split(":");
		  	}
		}
		console.log(arr2);
		var body = $('#bodyDetail');
		// 清除原有数据
		body.text('');
		$('tr.warehouseItem').each(function(){
			var warehouseCode = $(this).find('.isPrimary').val();
			if($(this).find('.bind').text() == '已绑定' && mainCode != warehouseCode) {
				var warehouseName = $(this).find('.warehouseName').text();
			    var str = "<tr><td>" + warehouseName + "<input type='hidden' class='warehouseCode' value='"
			            + warehouseCode + "'></td><td>";
			    var lostr = "<input type='text' class='level'></td></tr>";
			    for(var i=0;i<arr2.length;i++){
			    	if(warehouseCode == arr2[i][0] ){
			    		lostr = "<input type='text' class='level' value='" + arr2[i][1] + "'></td></tr>";
			    		break;
			    	}
			    }
			    body.append(str + lostr);
			}
		});
		// 数据加载结束，显示模态框
		$('#transfer').modal('show');
	}
	
	// 添加流转仓等级
	function addLevel(){
		var ary = new Array();
		var body = $('#bodyDetail');
		body.find('tr').each(function(){
		    var v = $(this).find('.level').val();
		    if(v != ''){
		      ary.push(v);
		    }
		});
		console.log(ary);
		// 重复等级判断
		var nary=ary.sort();
		for(var i=0;i<ary.length;i++){
			var numReg = new RegExp("^[0-9]*$");
			console.log(nary[i]);
			if(nary[i] != '' && !numReg.test(nary[i])){
				alert("请输入数字格式");
				return;
			}
			if (nary[i] == nary[i+1]){
				alert("等级不能重复："+nary[i]);
				return;
			}
		}
		
		// 封装提交的数据
		var data = [];
		data.push({
		  	"name" : "customerCode",
			"value": '${customer.customerCode }'
		});
		data.push({
		  	"name" : "warehouseCode",
			"value": parentWarehouseCode
		});
		var i = 0;
		body.find('tr').each(function(){
			data.push({
				"name" : "warehouseList[" + i +"].level",
				"value": $(this).find('.level').val()
			});
			data.push({
				"name" : "warehouseList[" + i +"].warehouseCode",
				"value": $(this).find('.warehouseCode').val()
			});
			i++;
		});
		$.ajax({
		    type : 'post',
			url : '${ctx}/v2/customers/addWarehouseLevel',
			data : data,
			success : function(data) {
			  if(data=='success') {
			  	  	alert("修改成功");
			  	  	location.reload(); 
			  	} else {
			  		alert("修改失败");
			  	}
			},
			error : function(data) {
			  	alert("网络繁忙，请稍后重试...","温馨提示");
			}
		});
	}
	
</script>
</head>

<body>
<section class="content">
 
 <div class="form-group row">
	<div class="col-md-12">
		<div class="box box-solid box-primary">
			<div class="box-header">
				<i class="fa fa-list"></i>
				<h3 class="box-title">绑定分仓</h3>
			</div>
			<div class="box-body">
				<font>是否启用分仓：</font>
				<input type="radio" name="isBranch" value='1' <c:if test="${customer.distributeWarehouseState == '1'}">checked</c:if> >启用
				<input type="radio" name="isBranch" value='0' <c:if test="${customer.distributeWarehouseState == '0'}">checked</c:if> >禁用
				<button class="btn btn-info" onclick="updateDistributeState()">确认</button>
				<br/>
				<font>商品库存查询：</font>
				<input type="radio" name="stockQueryType" value='0' <c:if test="${customer.stockQueryType == '0'}">checked</c:if> >所有仓库
				<input type="radio" name="stockQueryType" value='1' <c:if test="${customer.stockQueryType == '1'}">checked</c:if> >仓库编码
				<button class="btn btn-info" onclick="updateStockQueryType()">确认</button>
				<font>（所有仓库：不按报文的仓库编码查询，而去查询商家所有仓库的商品库存。仓库编码：按报文的仓库编码查询指定仓库的商品库存）</font>
			</div>
		</div>
	</div>
</div>
	
	<div class="form-group row">
		<div class="col-md-12">
			<div class="box box-solid box-primary">
				<div class="box-header">
					<i class="fa fa-list"></i>
					<h3 class="box-title">分仓设置</h3>
					<div class="box-tools pull-right">
						<button class="btn btn-primary btn-sm" data-widget="collapse">
							<i class="fa fa-minus"></i>
						</button>
					</div>
				</div>
				<div class="box-body">
					<table id="contentTable" class="table table-striped table-bordered table-condensed">
						<thead>
							<tr>					
							    <th style="width: 5%; text-align: center;">设为主仓</th>
							    <th style="width: 10%; text-align: center;">仓库名称</th>
							    <th style="width: 45%; text-align: center;">覆盖省份</th>
							    <th style="width: 15%; text-align: center;">下级流转仓</th>
							    <th style="width: 25%; text-align: center;">操作</th>
							</tr>
						</thead>
						<tbody>
							<c:forEach items="${warehouseList }" var="warehouse">
								<tr class="warehouseItem">
									<td style="text-align: center;">
										<input class="radio isPrimary" type="radio" name="iCheck" <c:if test="${warehouse.isPrimary == '1'}">checked</c:if>  value = "${warehouse.warehouseCode }">
									</td>
									<td class='warehouseName'>${warehouse.warehouseName }</td>
									<td>${warehouse.provinceList }</td>
									<td class="transferList">${warehouse.transferWarehouse }</td>
									<td>
										<a href="${ctx}/v2/customers/toCoverageArea?customerCode=${customer.customerCode }&warehouseCode=${warehouse.warehouseCode }" class="btn btn-primary">设置覆盖区</a>
										<a class='btn btn-primary' onclick="modeTransfer('${warehouse.warehouseCode }',this)">设置流转仓库</a>
										<c:if test="${warehouse.isBind == '1'}">
											<button class="btn disabled bind"  style="background-color: gray;">已绑定</button>
										</c:if>
										<c:if test="${warehouse.isBind == '0'}">
											<button class="btn btn-primary bind" onclick="bindWarehouse('${warehouse.warehouseCode}',this)">绑定分仓</button>
										</c:if>
									</td>
								</tr>
							</c:forEach>
						</tbody>
					</table>
					<button class="btn btn-info" onclick="addPrimaryWarehouse();">保存分仓</button>
				</div>
	</div>
	</div>
	</div>
	
	
	<!-- 模态窗口 start -->
	<div class="modal fade" role="dialog" id="transfer">
		<div class="modal-dialog">
			<div class="modal-content"  >
	
		<!-- 头 -->
		<div class="modal-header">
			<span style="font-size: large; font-weight: bold;" id="windowTitle"></span>
		</div>
		<!-- 体 -->
		<div class="modal-body">
			<div class="row">
				<div class="col-md-12">
				<!-- Primary box -->
				<div class="box box-solid box-primary">
					<div class="box-header">
						<i class="fa fa-list"></i>
						<h3 class="box-title">设置流转仓库</h3>
					</div>
					<div class="box-body">
						<table id="contentTable" name="contentTable"
							class="table table-striped table-bordered table-condensed">
							<thead>
								<tr>
									<th>仓库名称</th>
									<th>优先级</th>
								</tr>
							</thead>
							<tbody id="bodyDetail">
							</tbody>
						</table>
						<font>优先级请填不重复数字，如果不填则表示此仓库不作为流转仓</font>
					</div>
				</div>
			</div>
		</div>
		</div>
		<!-- 尾 -->
		<div class="modal-footer bg-gray">
			<input class="btn btn-primary" type="button" onclick="addLevel()" value="保存设置"/>
			<input class="btn btn-primary" type="button" value="返回"  data-dismiss="modal" style="padding-left: 10px;"/>
		</div>
		</div>
		</div>
     </div>
     <!-- 模态窗口 end -->
</section>	
</body>
</html>
